import 'package:flutter/material.dart';
import 'package:flutter_default_project/add_todo_screen.dart';
import 'package:flutter_default_project/home_screen.dart';
import 'package:key_value_store_flutter/key_value_store_flutter.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:todos_app_core/todos_app_core.dart';
import 'package:todos_repository_core/todos_repository_core.dart';
import 'package:todos_repository_local_storage/todos_repository_local_storage.dart';

import 'todo_list_model.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(
    ProviderApp(
      repository: LocalStorageRepository(
        localStorage: KeyValueStorage(
          'change_notifier_provider_todos',
          FlutterKeyValueStore(await SharedPreferences.getInstance()),
        ),
      ),
    ),
  );
}

class ProviderApp extends StatelessWidget {
  final TodosRepository repository;
  const ProviderApp({super.key, required this.repository});

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => TodoListModel(repository: repository)..loadTodos(),
      child: MaterialApp(
        routes: {
          ArchSampleRoutes.home: (context) => const HomeScreen(),
          ArchSampleRoutes.addTodo: (context) => const AddTodoScreen(),
        },
      ),
    );
  }
}
